// pages/report/report.js
const { formatDate } = require('../../utils/util.js')

Page({
  data: {
    reportType: 'daily',
    reportTitle: '',
    reportDate: '',
    reportData: {
      totalTasks: 0,
      completedTasks: 0,
      completionRate: 0,
      totalTime: 0,
      tasks: [],
      avgTime: 0,
      bestTime: '',
      focusRate: 0,
      summary: '',
      suggestions: []
    }
  },

  onLoad(options) {
    const { type = 'daily' } = options
    this.setData({ reportType: type })
    this.generateReport()
  },

  generateReport() {
    const type = this.data.reportType
    let title = ''
    let dateText = ''

    const today = new Date()
    
    switch (type) {
      case 'daily':
        title = '今日工作日报'
        dateText = formatDate(today)
        break
      case 'weekly':
        title = '本周工作周报'
        dateText = this.getWeekRange(today)
        break
      case 'monthly':
        title = '本月工作月报'
        dateText = today.getFullYear() + '年' + (today.getMonth() + 1) + '月'
        break
    }

    // 模拟报告数据
    const reportData = {
      totalTasks: 8,
      completedTasks: 6,
      completionRate: 75,
      totalTime: 240,
      tasks: [
        {
          id: 1,
          title: '完成项目需求分析',
          completed: true,
          startTime: '09:00',
          endTime: '10:30',
          duration: 90
        },
        {
          id: 2,
          title: '开发任务管理功能',
          completed: true,
          startTime: '10:45',
          endTime: '12:00',
          duration: 75
        },
        {
          id: 3,
          title: '编写技术文档',
          completed: false,
          startTime: '14:00',
          endTime: null,
          duration: null
        },
        {
          id: 4,
          title: '团队会议讨论',
          completed: true,
          startTime: '15:30',
          endTime: '16:30',
          duration: 60
        }
      ],
      avgTime: 40,
      bestTime: '10:00-12:00',
      focusRate: 85,
      summary: '今日工作表现良好，完成了大部分计划任务。在上午时段工作效率较高，下午需要进一步提升专注度。',
      suggestions: [
        '合理安排任务优先级，先完成重要任务',
        '避免多任务并行，提高单任务完成质量',
        '适当安排休息时间，保持工作节奏'
      ]
    }

    this.setData({
      reportTitle: title,
      reportDate: dateText,
      reportData
    })
  },

  getWeekRange(date) {
    const day = date.getDay()
    const diff = date.getDate() - day + (day === 0 ? -6 : 1)
    const monday = new Date(date.setDate(diff))
    const sunday = new Date(date.setDate(diff + 6))
    
    return formatDate(monday) + ' 至 ' + formatDate(sunday)
  },

  onShare() {
    wx.showShareMenu({
      withShareTicket: true
    })
    
    wx.showToast({
      title: '准备分享...',
      icon: 'success'
    })
  },

  onSave() {
    wx.showToast({
      title: '日报已保存',
      icon: 'success'
    })
  }
})